package cn.alpha.intell.rbtlib.share;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.List;

import cn.alpha.intell.rbtlib.R;
import cn.alpha.intell.rbtlib.manager.image.main.ImageLoaderFactory;


/**
 * Created by zhangxiliang on 2016/10/28.
 */
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

    private Context mContext;
    private LayoutInflater inflate;
    private List<Contact> mDataList;

    private int mSelected=-1;
    public OnItemClickListener onItemClickListener;
    public boolean isEdit;

    public ContactAdapter(Context context, List<Contact> userInfoList) {
        this.mContext = context;
        inflate = LayoutInflater.from(
                mContext);
        this.mDataList = userInfoList;
    }


    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflate.inflate(R.layout.item_contact, parent,
                false);
        ContactViewHolder holder = new ContactViewHolder(view, viewType);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ContactViewHolder holder, final int position) {

        final Contact info = mDataList.get(position);

        ImageLoaderFactory.getInstance().loadImage(holder.mHeadIv, info.getAvatar());

        holder.mRelationTv.setText(info.getRelation());
        holder.nameTv.setText(info.getUserName());

        if (mSelected == position) {
            holder.selectedIv.setVisibility(View.VISIBLE);
        } else {
            holder.selectedIv.setVisibility(View.GONE);

        }

        if (info.getRelation()==null) {
            holder.mRelationTv.setText("朋友");
            holder.mRelationTv.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.drawable.rbt_user), null, null, null);
        } else {
            holder.mRelationTv.setText(info.getRelation());
            holder.mRelationTv.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.drawable.mob_user), null, null, null);
        }


        holder.mHeadIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(info,position);
                }
                mSelected = position;
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        if (mDataList != null) {
            return mDataList.size();
        }
        return 0;
    }

    class ContactViewHolder extends RecyclerView.ViewHolder {

        CircleImageView mHeadIv;
        TextView mRelationTv;
        TextView nameTv;
        ImageView selectedIv;


        public ContactViewHolder(View itemView, int layoutType) {
            super(itemView);
            mHeadIv = (CircleImageView) itemView.findViewById(R.id.contact_iv);
            mRelationTv = (TextView) itemView.findViewById(R.id.contact_relation_tv);
            nameTv = (TextView) itemView.findViewById(R.id.contact_name_tv);
            selectedIv = (ImageView) itemView.findViewById(R.id.contact_select_iv);


        }


    }


    public Contact getSelectedContact(){
        return mDataList.get(mSelected);
    }



    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }


    public interface OnItemClickListener {
        public void onItemClick(Contact info, int position);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
